# -*- coding: utf-8 -*-
import random
import time
from DrissionPage import Chromium


browser = Chromium(9233)
tab1 = browser.latest_tab
# tab1.get("https://www.procell.com.cn/search-keywords=PM150410")
# tab1.get("https://www.procell.com.cn/p/mem-with-neaa-pm150410-71162")


def get_product_url():
    # 获取商品详情url
    item_dom = tab1.ele("xpath:/html/body/main/section[2]/div/div[1]/div[1]/div/a")
    if item_dom:
        url = item_dom.attr("href")
        return url
    return None


def get_product_detail():
    item_dom = tab1.ele("xpath://table[@class='detail-table']")
    return item_dom.inner_html


def get_product_image():
    img = tab1.ele("xpath:/html/body/main/section[3]/div/div[1]/div[2]/div[1]/div/img")
    if img:
        return img.attr("src")
    return "https://bio-mercury-1316961827.cos.ap-guangzhou.myqcloud.com/photos/procell/procell.png"


def process_data(procode):
    search_url = f"https://www.procell.com.cn/search-keywords={procode}"
    tab1.get(search_url)
    product_url = get_product_url()
    if product_url:
        time.sleep(random.randint(3, 5))
        tab1.get(product_url)
        product_detail = get_product_detail()
        product_image = get_product_image()
        return product_detail, product_image
    return None, None
